package com.pro.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.pro.dao.UserMapper;
import com.pro.dao.WxUserMsgMapper;
import com.pro.pojo.User;
import com.pro.pojo.WxUserMsg;
import com.pro.service.UserService;

@Service
public class UserServiceImpl implements UserService {
	
	@Resource
	UserMapper userMapper;
	@Resource
	WxUserMsgMapper wxUserMsgMapper;
	
	@Override
	public User login(String username, String password) throws Exception {
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("username", username);
		condition.put("password", password);
		List<User> result = userMapper.getUserByCondition(condition);
		return result == null || result.isEmpty()? null: result.get(0);
	}

	@Override
	public User apply(User user) throws Exception {
		
		userMapper.saveSelective(user);
		User us=userMapper.selectUser(user).get(0);
		return us;
	}

	@Override
	public void saveWxUser(WxUserMsg umsg) throws Exception {
		if(wxUserMsgMapper.selectWxUserByOpenid(umsg)==null){
			wxUserMsgMapper.saveWxUser(umsg);
		}
		
	}

	@Override
	public List<User> selectByOpenId(String openid) throws Exception {
		List<User> us=(List<User>) userMapper.selectByOpenid(openid);
		return us;
	}

	@Override
	public int selectCount(User user) throws Exception{
		int count=userMapper.selectCount(user);
		return count;
	}

	@Override
	public WxUserMsg selectWxUser(String openid) throws Exception {
		WxUserMsg use = wxUserMsgMapper.selectWxUser(openid);
		return use;
	}

	@Override
	public void deleteByOpenid(String openid) throws Exception {
		 userMapper.deleteUserByOpenid(openid);
		
	}

	@Override
	public int selectCountByOpenid(User user) throws Exception {
		int count=userMapper.selectCountByOpenid(user);
		return count;
	}

	@Override
	public User selectUser(User user) throws Exception {
		User use= userMapper.selectByUser(user);
		return use;
	}

	@Override
	public void updateUser(User user) throws Exception {
		userMapper.updateUser(user);
		
	}

}
